package com.eascs.admin.sys.permission;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.sys.permission.trans.RsourcesTransSysRsources;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.tag.ActPermissionService;
import com.eascs.tag.entity.Resources;
import com.eascs.workflow.common.entity.sys.SysResources;
import com.eascs.workflow.common.entity.sys.SysUser;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;

/**
 * eg:权限测试：<@permission permissionName="selectCustomer"> 显示</@permission>
 */
@Service
public class ActPermissionServiceImpl implements ActPermissionService {
	private static Logger log = LoggerFactory.getLogger(ActPermissionServiceImpl.class);
	@Reference
	private SysPermissionService sysPermissionService;

	public ModelResult<List<SysResources>> queryResourcesByUser(SysUser user) {
		return sysPermissionService.queryResourcesByUser(user);
	}

	@Override
	public List<Resources> queryResourcesByUserno(String userNo) {
		SysUser user = new SysUser();
		user.setEmployeeNo(Integer.valueOf(userNo));
		ModelResult<List<SysResources>> modelResult = sysPermissionService.queryResourcesByUser(user);
		List<SysResources> list = modelResult.getModel();

		List<Resources> result = new ArrayList<>();
		if (null == list)
			return result;

		for (SysResources item : list) {
			Resources resObj = new Resources();
			resObj.setId(item.getId());
			resObj.setResourceCode(item.getResourceCode());
			resObj.setDescription(item.getDescription());
			result.add(resObj);
		}
		return result;
	}
	@Override
	public List<Resources> queryResourcesStartWithByCODE(String code) {
		// TODO Auto-generated method stub
		List<SysResources> sysResources=sysPermissionService.selectResourcesListByCode(code);
		List<Resources> ss=null;
		if (sysResources!=null) {
			if (sysResources.size()>0) {
				ss=new ArrayList<Resources>();
				String ssParentId=sysResources.get(0).getParentId();
				if ("-1".equals(ssParentId)) {
					return ss;
				}else{
					ss.add(RsourcesTransSysRsources.rsTsysrs(sysResources.get(0)));
				}
				for (int i = 0; i < 10; i++) {
					SysResources sysResource = sysPermissionService.getResourcesByIds(ssParentId);
					ssParentId = sysResource.getParentId();
					if ("-1".equals(ssParentId)) {
						break;
					} else {
						ss.add(RsourcesTransSysRsources.rsTsysrs(sysResource));
					}
				}
			}
		}
		return ss;
	}
}
